package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsSiteService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsSiteDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsSite.
 */
@RestController
@RequestMapping("/api")
public class CmsSiteResource {

    private final Logger log = LoggerFactory.getLogger(CmsSiteResource.class);
        
    @Inject
    private CmsSiteService cmsSiteService;

    /**
     * POST  /cms-sites : Create a new cmsSite.
     *
     * @param cmsSiteDTO the cmsSiteDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsSiteDTO, or with status 400 (Bad Request) if the cmsSite has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-sites")
    @Timed
    public ResponseEntity<CmsSiteDTO> createCmsSite(@Valid @RequestBody CmsSiteDTO cmsSiteDTO) throws URISyntaxException {
        log.debug("REST request to save CmsSite : {}", cmsSiteDTO);
        if (cmsSiteDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsSite", "idexists", "A new cmsSite cannot already have an ID")).body(null);
        }
        CmsSiteDTO result = cmsSiteService.save(cmsSiteDTO);
        return ResponseEntity.created(new URI("/api/cms-sites/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsSite", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-sites : Updates an existing cmsSite.
     *
     * @param cmsSiteDTO the cmsSiteDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsSiteDTO,
     * or with status 400 (Bad Request) if the cmsSiteDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsSiteDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-sites")
    @Timed
    public ResponseEntity<CmsSiteDTO> updateCmsSite(@Valid @RequestBody CmsSiteDTO cmsSiteDTO) throws URISyntaxException {
        log.debug("REST request to update CmsSite : {}", cmsSiteDTO);
        if (cmsSiteDTO.getId() == null) {
            return createCmsSite(cmsSiteDTO);
        }
        CmsSiteDTO result = cmsSiteService.save(cmsSiteDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsSite", cmsSiteDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-sites : get all the cmsSites.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsSites in body
     */
    @GetMapping("/cms-sites")
    @Timed
    public List<CmsSiteDTO> getAllCmsSites() {
        log.debug("REST request to get all CmsSites");
        return cmsSiteService.findAll();
    }

    /**
     * GET  /cms-sites/:id : get the "id" cmsSite.
     *
     * @param id the id of the cmsSiteDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsSiteDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-sites/{id}")
    @Timed
    public ResponseEntity<CmsSiteDTO> getCmsSite(@PathVariable Long id) {
        log.debug("REST request to get CmsSite : {}", id);
        CmsSiteDTO cmsSiteDTO = cmsSiteService.findOne(id);
        return Optional.ofNullable(cmsSiteDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-sites/:id : delete the "id" cmsSite.
     *
     * @param id the id of the cmsSiteDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-sites/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsSite(@PathVariable Long id) {
        log.debug("REST request to delete CmsSite : {}", id);
        cmsSiteService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsSite", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-sites?query=:query : search for the cmsSite corresponding
     * to the query.
     *
     * @param query the query of the cmsSite search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-sites")
    @Timed
    public List<CmsSiteDTO> searchCmsSites(@RequestParam String query) {
        log.debug("REST request to search CmsSites for query {}", query);
        return cmsSiteService.search(query);
    }


}
